package com.csust.automaticteach.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.generatequestion.AddGenerateQuestionRequest;
import com.csust.automaticteach.model.dto.generatequestion.GenerateQuestionQueryDTO;
import com.csust.automaticteach.model.dto.generatequestion.JudgeGenerateQuestionRequest;
import com.csust.automaticteach.model.entity.Exercises;
import com.csust.automaticteach.model.entity.GenerateQuestion;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.generatequestion.GenerateQuestionPageVO;
import com.csust.automaticteach.model.vo.generatequestion.GenerateQuestionVO;
import com.csust.automaticteach.model.vo.generatequestion.JudgeGenerateQuestionVO;
import com.csust.automaticteach.service.GenerateQuestionService;
import com.csust.automaticteach.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassNameGenerateQuestionController
 * @Description 题目生成的控制器
 * @Author钰玟
 * @Date2025/6/20 上午10:56
 * @Version 1.0
 **/
@RestController
@RequestMapping("/generateQuestion")
//@Tag(name = "生成题目相关接口")
public class GenerateQuestionController {

    @Resource
    private GenerateQuestionService generateQuestionService;
    @Resource
    private UserService userService;

    /**
     * 生成练习题目
     *
     * @param addGenerateQuestionRequest 添加题目请求
     * @param request                    请求
     * @return List<GenerateQuestionVO>
     */
    @PostMapping("/getGenerateQuestion")
    @Deprecated //统一接口在GenerateExercisesController中     @PostMapping("/question/add")
    public BaseResponse<List<Exercises>> GenerateQuestion(@RequestBody AddGenerateQuestionRequest addGenerateQuestionRequest
            , HttpServletRequest request) {
        //校验参数
        ThrowUtils.throwIf(addGenerateQuestionRequest == null, ErrorCode.PARAMS_ERROR);
        //获取用户
        User loginUser = userService.getLoginUser(request);
//        List<GenerateQuestionVO> generateQuestionVOs = generateQuestionService.getGenerateQuestion(addGenerateQuestionRequest, loginUser);
        List<Exercises> exercisesList = generateQuestionService.getGenerateQuestionRefactoring(addGenerateQuestionRequest, loginUser);
        return ResultUtils.success(exercisesList);
    }

    @PostMapping("/judge_question")
    @Deprecated //统一接口在GenerateExercisesController中      @GetMapping(value = "/judge/stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public BaseResponse<List<JudgeGenerateQuestionVO>> judgeQuestion(@RequestBody JudgeGenerateQuestionRequest judgeGenerateQuestionRequest) {
        //校验参数
        ThrowUtils.throwIf(judgeGenerateQuestionRequest == null, ErrorCode.PARAMS_ERROR);
//        List<JudgeGenerateQuestionVO> judgeGenerateQuestionVOS = generateQuestionService.judgeGenerateQuestion(judgeGenerateQuestionRequest);
        List<JudgeGenerateQuestionVO> judgeGenerateQuestionVOS = new ArrayList<>();
        return ResultUtils.success(judgeGenerateQuestionVOS);
    }

    /**
     * 分页获取题目列表
     *
     * @param generateQuestionQueryDTO 查询条件
     * @param request                  请求
     * @return Page<GenerateQuestionPageVO>
     */
    @PostMapping("/list/page/vo")
    @Deprecated //统一接口在GenerateExercisesController中       @PostMapping("/question/list/page")
    public BaseResponse<Page<GenerateQuestionPageVO>> listGenerateQuestionVOByPage(@RequestBody GenerateQuestionQueryDTO generateQuestionQueryDTO
            , HttpServletRequest request) {
        long current = generateQuestionQueryDTO.getCurrent();
        long pageSize = generateQuestionQueryDTO.getPageSize();
        User loginUser = userService.getLoginUser(request);
        //设置用户Id
        generateQuestionQueryDTO.setUserId(loginUser.getId());
        Page<GenerateQuestion> generateQuestionPage = generateQuestionService.page(new Page<>(current, pageSize)
                , generateQuestionService.getGenerateQuestionQueryWrapper(generateQuestionQueryDTO));
        long total = generateQuestionPage.getTotal();
        Page<GenerateQuestionPageVO> generateQuestionPageVOPage = new Page<>(current, pageSize, total);
        List<GenerateQuestionPageVO> generateQuestionPageVOList = generateQuestionService.getGenerateQuestionPageVOList(generateQuestionPage.getRecords());
        generateQuestionPageVOPage.setRecords(generateQuestionPageVOList);
        return ResultUtils.success(generateQuestionPageVOPage);
    }


    /**
     * 获取题目
     *
     * @param id 题目id
     * @return GenerateQuestionVO
     */
    @GetMapping("/get/{id}")
    @Deprecated  // 移至 GenerateExercisesController下的     @GetMapping("/question/get")
    public BaseResponse<List<GenerateQuestionVO>> getGenerateQuestionVO(@PathVariable("id") long id) {
        GenerateQuestion generateQuestion = generateQuestionService.getById(id);
        List<GenerateQuestionVO> generateQuestionVOList = generateQuestionService.getGenerateQuestionVOList(generateQuestion);
        return ResultUtils.success(generateQuestionVOList);
    }


    /**
     * 删除生成题目
     *
     * @param deleteRequest 题目id
     * @return GenerateQuestionVO
     */
    @PostMapping("/delete")
    @Deprecated  // 移至 GenerateExercisesController下的     @PostMapping("/question/delete")
    public BaseResponse<Boolean> deleteGenerateQuestionDeprecated(@RequestBody DeleteRequest deleteRequest) {
        //校验参数
        ThrowUtils.throwIf(deleteRequest == null, ErrorCode.PARAMS_ERROR);
        boolean b = generateQuestionService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

}
